package com.SmartCare.controller.manager;


import com.SmartCare.common.Result;
import com.SmartCare.pojo.role.User;
import com.SmartCare.service.role.UserService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表 前端控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(tags = "后台管理端用户接口")
public class UserController {
    @Resource
    UserService userService;

    //登录
    @PostMapping("/login")
    @Operation(summary = "后台登录", description = "根据用户名或手机号登入后台管理系统。参数需在RequestBody中传入用户对象，对象中password必传，name和phone传入其中之一，分别代表通过用户名和手机号登录。其余用户属性不需传入。")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    //注册
    @PostMapping("/register")
    @Operation(summary = "后台注册", description = "注册一个后台管理系统用户。参数需在RequestBody中传入用户对象，对象中必须传入name，phone，password属性，其余用户属性不可不传入。")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    //查询所有用户（非管理员）
    @GetMapping("/listUsers")
    @Operation(summary = "查询用户", description = "查询所有非管理员用户")
    public Result listUsers(@Parameter(description = "具体查询的页数", required = true) @RequestParam Integer pageNum,
                            @Parameter(description = "每页数据的条数", required = true) @RequestParam Integer pageSize) {
        return userService.listUsers(pageNum, pageSize, true);
    }

    //查询所有管理员
    @GetMapping("/listAdmins")
    @Operation(summary = "查询管理员", description = "查询所有管理员用户")
    public Result listAdmins(@Parameter(description = "具体查询的页数", required = true) @RequestParam Integer pageNum,
                             @Parameter(description = "每页数据的条数", required = true) @RequestParam Integer pageSize) {
        return userService.listUsers(pageNum, pageSize, false);
    }

    //根据不同的字段搜索用户
    @GetMapping("/searchUser")
    @Operation(summary = "搜索用户", description = "根据不同的字段搜索用户")
    public Result searchUser(@Parameter(description = "具体查询的页数", required = true) @RequestParam Integer pageNum,
                             @Parameter(description = "每页数据的条数", required = true) @RequestParam Integer pageSize,
                             @Parameter(description = "搜索的关键词", required = true) @RequestParam String keyword,
                             @Parameter(description = "搜索依据的字段名(要与数据库中一致)", required = true) @RequestParam String type) {
        return userService.search(pageNum, pageSize, keyword, type, true);
    }

    //根据不同的字段搜索管理员
    @GetMapping("/searchAdmin")
    @Operation(summary = "搜索管理员", description = "根据不同的字段搜索管理员")
    public Result searchAdmin(@Parameter(description = "具体查询的页数", required = true) @RequestParam Integer pageNum,
                              @Parameter(description = "每页数据的条数", required = true) @RequestParam Integer pageSize,
                              @Parameter(description = "搜索的关键词", required = true) @RequestParam String keyword,
                              @Parameter(description = "搜索依据的字段名(要与数据库中一致)", required = true) @RequestParam String type) {
        return userService.search(pageNum, pageSize, keyword, type, false);
    }

    //添加或修改用户信息
    @PostMapping("/save")
    @Operation(summary = "添加或修改用户", description = "传入User对象，如果包含userId字段则代表修改用户，不传入userId则代表新增用户")
    public Result save(@RequestBody User user) {
        return userService.saveUser(user);
    }

    //根据id启用或禁用单个用户
    @PutMapping("/enable/{id}")
    @Operation(summary = "启用或禁用单个用户", description = "修改用户的Status属性")
    public Result enable(@Parameter(description = "用户的ID", required = true) @PathVariable Long id,
                         @Parameter(description = "启用插入true，禁用传入false", required = true) @RequestParam Boolean enable) {
        return userService.enable(id, enable);
    }

    //根据id删除单个用户
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除用户", description = "根据id来删除指定用户")
    public Result deleteUser(@Parameter(description = "用户的ID", required = true) @PathVariable Long id) {
        return userService.deleteUser(id);
    }

    //批量启用或禁用
    @PutMapping("/enable-batch")
    @Operation(summary = "批量启用或禁用用户", description = "根据传入的id序列逐个修改用户的Status属性。请求体中传入要进行操作的用户id数组")
    public Result enableBatch(@Parameter(description = "禁用或启用", required = true) @RequestParam Boolean enable,
                              @Parameter(description = "用户的ID数组", required = true) @RequestBody List<Long> ids) {
        return userService.enableBatch(enable, ids);
    }

    //批量删除用户
    @DeleteMapping("/delete-batch")
    @Operation(summary = "批量删除用户", description = "根据传入的id序列逐个删除用户。请求体中传入要进行操作的用户id数组")
    public Result deleteBatch(@Parameter(description = "用户的ID数组字符串", required = true) @RequestParam String ids) {
        return userService.deleteBatch(ids);
    }

    //修改密码
    @PutMapping("/change-password")
    public Result changePassword(@RequestParam String mail, @RequestParam String password) {
        return userService.changePassword(mail, password);
    }

    //根据养老院获取所有护工
    @GetMapping("/query-worker")
    public Result getAllWorker(@RequestParam String careHomeName){
        return userService.getWorkerList(careHomeName);
    }

    //根据养老院获取全部用户
    @GetMapping("/query-by-care-home")
    public Result getAllUser(@RequestParam String name){
        return userService.getAllUser(name);
    }

    @PostMapping("/set-user-data")
    public Result setUserData(@RequestParam String name, @RequestParam String phone, @RequestParam Long id){
        return userService.setUserData(id, name, phone);
    }

}

